package com.lin.carbon.controller;

import com.lin.carbon.common.Result;
import com.lin.carbon.entity.SysDictItemVO;
import com.lin.carbon.service.SysDictItemService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SysDictItem")
@CrossOrigin
public class SysDictItemController {

    private final SysDictItemService sysDictItemService;

    @Autowired
    public SysDictItemController(SysDictItemService sysDictItemService) {
        this.sysDictItemService = sysDictItemService;
    }

    /**
     * 分页全查询接口
     */
    @GetMapping("/getAll")
    public Result<Page<SysDictItemVO>> getAllDictItems(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<SysDictItemVO> page = sysDictItemService.getAllDictItems(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 按ID查询接口
     */
    @GetMapping("/getById")
    public Result<SysDictItemVO> getDictItemById(@RequestParam Long id) {
        SysDictItemVO sysDictItemVO = sysDictItemService.getDictItemById(id);
        return Result.success(sysDictItemVO);
    }

    /**
     * 根据字典编码查询字典项的字典编码和中文描述
     * 查询条件：dictCode
     * 返回结果：List<dictCode, DictName>>
     */
    @GetMapping("/getNameListByDictCode/{dictCode}")
    public Result<List<Map<String, Object>>> getNameListByDictCode(@PathVariable String dictCode) {
        List<Map<String, Object>> nameList = sysDictItemService.selectNameListByDictCode(dictCode);
        return Result.success(nameList);
    }

    /**
     * 通过dictCode获取系统字典配置的分页列表（子集字典sys_dict_item）
     */
    @PostMapping("/dictConfig/getPageList")
    public Result<Page<SysDictItemVO>> getDictItemPageListByDictCode(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String dictCode) {
        Page<SysDictItemVO> page = sysDictItemService.getDictItemPageListByDictCode(pageNum, pageSize, dictCode);
        return Result.success(page);
    }


    /**
     * 添加系统字典条目
     * @param sysDictItemVO 系统字典条目对象
     * @return 统一响应结果，操作成功返回 true，失败返回 false
     */
    @PostMapping("/dictConfig/add")
    public Result<Boolean> addDictItem(@RequestBody SysDictItemVO sysDictItemVO) {
        boolean result = sysDictItemService.addDictItem(sysDictItemVO);
        return Result.success(result);
    }


    /**
     * 修改系统字典条目
     * @param sysDictItemVO 包含修改信息的系统字典条目对象
     * @return 统一响应结果，操作成功返回 true，失败返回 false
     */
    @PutMapping("/dictConfig/update")
    public Result<Boolean> updateDictItem(@RequestBody SysDictItemVO sysDictItemVO) {
        boolean result = sysDictItemService.updateDictItem(sysDictItemVO);
        return Result.success(result);
    }

    /**
     * 删除系统字典条目
     */
    @DeleteMapping("/dictConfig/delete/{id}")
    public ResponseEntity<Boolean> deleteDictItemById(@PathVariable Long id) {
        boolean result = sysDictItemService.deleteDictItemById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取 dict_code 为 012 且 item_ch 中包含“市”字的城市字典数据
     * @return 统一响应结果，包含符合条件的城市字典列表
     */
    @GetMapping("/getCityDict")
    public Result<List<SysDictItemVO>> getCityDict() {
        List<SysDictItemVO> cityDictList = sysDictItemService.getCityDict();
        return Result.success(cityDictList);
    }

    @GetMapping("/getFieldDict")
    public Result<List<SysDictItemVO>> getFieldDict() {
        List<SysDictItemVO> fieldDictList = sysDictItemService.getFieldDict();
        return Result.success(fieldDictList);
    }

}
